const express = require('express');

const router = express.Router();
//导入数据库操作模块
const db = require('../db/index');

//导入生成token包
const jwt = require('jsonwebtoken');
const config = require('../config/config');


//注册新用户处理函数
router.post('/register', (req, res) => {
    //获取用户提交的用户信息
    const userinfo = req.body.userinfo;

    if (!userinfo.username || !userinfo.password) {
        return res.send({ code: 400, message: '账号和密码不能为空' });
    }

    const selectSql = 'select * from user where username=?'
    db.query(selectSql, userinfo.username, (err, results) => {
        if (err) return res.cc(err)

        if (results.length > 0) return res.cc('该账号已存在')

        //插入新用户sql语句
        const insertSql = 'insert into user set ?'
        db.query(insertSql, { username: userinfo.username, password: userinfo.password, nickname: userinfo.nickname }, (err, results) => {
            //判读sql语句是否执行成功
            if (err) return res.cc(err)

            //判断影响行数是否为1
            if (results.affectedRows !== 1) return res.cc('注册失败，请稍后重新注册')

            //注册成功
            res.send({ code: 200, message: '注册成功' })
        })
    })
})


//登录处理函数
router.post('/login', (req, res) => {
    console.log(req.body);
    const userinfo = req.body.userinfo

    if (!userinfo.username || !userinfo.password) {
        return res.cc('请输入账号和密码')
    }

    const selectSql = 'select * from user where username=?'
    db.query(selectSql, userinfo.username, (err, results) => {
        if (err) return res.cc(err)

        if (results.length === 0) return res.cc('账号不存在')

        if (results.length > 1) return res.cc('账号异常，请联系管理员')

        if (results[0].password !== userinfo.password) {
            return res.cc('账号或密码错误')
        }

        //将用户的敏感信息去除 密码和用户头像置空
        const user = { ...results[0], password: '', userpic: '' }
        //对用户的信息进行加密 生成token字符串
        const tokenStr = jwt.sign(user, config.jwtSecretKey, { expiresIn: config.expiresIn })

        res.send({
            code: 200,
            message: '登录成功',
            userinfo: user,
            token: 'Bearer ' + tokenStr
        })
    })
})

//管理员登录
router.post('/adLogin', (req, res) => {

    const userinfo = req.body.userinfo
    console.log(userinfo);
    if (!userinfo.username || !userinfo.password) {
        return res.cc('请输入账号和密码')
    }

    const selectSql = 'select * from user where username=? and type != 0'
    db.query(selectSql, userinfo.username, (err, results) => {
        if (err) return res.cc(err)

        if (results.length === 0) return res.cc('账号不存在')

        if (results.length > 1) return res.cc('账号异常，请联系管理员')

        if (results[0].password !== userinfo.password) {
            return res.cc('账号或密码错误')
        }

        //将用户的敏感信息去除 密码和用户头像置空
        const user = { ...results[0], password: '', userpic: '' }
        //对用户的信息进行加密 生成token字符串
        const tokenStr = jwt.sign(user, config.jwtSecretKey, { expiresIn: config.expiresIn })

        res.send({
            code: 200,
            message: '登录成功',
            userinfo: user,
            token: 'Bearer ' + tokenStr
        })
    })
})

module.exports = router;